package com.homesoft.runtime.metadata.modle;

import java.io.File;

import com.homesoft.runtime.metadata.engine.MetadataEngineFactory;

public class RootObjectTypeHelper {
	
	private static String[] extensions = null;
	
	/**
	 * 获取某种类型的元数据文件扩展名
	 * @return
	 */
    public static String getExtension(RootObjectType type) {
        return getExtension(type.toString());
    }

    /**
     * 根据元数据文件名得到元数据类型
     * @param fileName
     * @return
     */
    public static RootObjectType GetRootObjectType(String fileName) {
        int index = fileName.indexOf(MetadataEngineFactory.METADATASTR);
        String typeName = fileName.substring(index + MetadataEngineFactory.METADATASTR.length());
        
        return Enum.valueOf(RootObjectType.class, typeName);
    }

    /**
     * 根据类型名称得到元数据文件扩展名
     * @param typeName
     * @return
     */
    public static String getExtension(String typeName) {
        return MetadataEngineFactory.METADATASTR + typeName.toLowerCase();
    }

    /**
     * 判读是否元数据文件（根据文件名的约定）
     * @param fileName
     * @return
     */
    public static boolean IsMetadataFile(String fileName) {
        return fileName.indexOf(MetadataEngineFactory.METADATASTR) > 0;
    }
    
    /**
     * 获取所有类型的扩展名
     */
    public static String[] getExtensions() {
        if (extensions == null) {
            RootObjectType[] values = RootObjectType.values();
            extensions = new String[values.length];
            
            for (int i = 0; i < values.length; i++) {
                extensions[i] = getExtension(values[i].toString());
            }
        }
        
        return extensions;
    }

    /**
     * 获取元数据文件对应的元数据对象的全名称
     * @param fileName
     * @return
     */
    public static String GetRootObjectFullName(String fileName)
    {
    	File file = new File(fileName);
        fileName = file.getName();
        int index = fileName.indexOf(MetadataEngineFactory.METADATASTR);
        
        if (index > 0) {
            return fileName.substring(0, index);
        }
        
        return null;
    }
}